<?php
/**
 * 微信
 */
namespace Weixin\Controller;
use Common\Controller\WeixinbaseController;

class IndexController extends WeixinbaseController {
    protected $wx_user;
    protected $wx_model;
    public function _initialize() {
        $this->wx_user = D("Common/WxUser");
	    $this->wx_model = D("Common/WxConfig");
    }
    public function index(){
        $config = $this->wx_model->where("id=1")->find();
        $weixin = new \Weixin\Wechat($config);
        $weixin->valid();
        $type = $weixin->getRev()->getRevType();
        $wxid = $weixin->getRev()->getRevFrom();
        //上报地理信息
        $eventType = $weixin->getRev()->getRevEvent();
        if("LOCATION" == $eventType['event']){
        	$loc = $weixin->getRev()->getUserLocation();
        	if($loc){
        		$this->updatelocation($wxid, $loc);
        	}
        } 
       
        switch($type) {
            case 'text':
                $content = $weixin->getRev()->getRevContent();
                break;
            case 'event':
                $event = $weixin->getRev()->getRevEvent();
                $content =  json_encode($event);
                break;
            case 'image':
                $content = json_encode($weixin->getRev()->getRevPic());
                break;
            case 'location':
                $content = json_encode($weixin->getRev()->getRevGeo());
                break;
        }
        $revFrom = $weixin->getRev()->getRevFrom();
        $revTo = $weixin->getRev()->getRevTo();
        if(!empty($revFrom) && !empty($revTo)){
        	
            if ($event['event'] == "subscribe") { //关注推送事件
            	
            	
                //用户关注
                $followInfo = $this->wx_user->where("open_id='$wxid'")->find();
                if(!$followInfo or $followInfo['expire_in']-86400<time()){
                    $info = $weixin->getUserInfo($wxid);
	                if ( strpos($event['key'],"qrscene_") !== false ) { //扫推广二维码关注
	            		   $ticket = substr($event['key'],8);
	            		   $info['promoters_id'] =  $ticket;
	            	}
                    if($info) $this->followUser($wxid,$info);
                }
                echo $weixin->text($config['bindmsg'])->reply();//发送欢迎信息
                exit;
            }
            if ($event['event'] == "unsubscribe"){    //用户取消关注
                exit;
            }
            //判断用户是否点击的菜单
            if ($event['event'] == "CLICK"){
                echo $weixin->text("未定义菜单事件")->reply();
                exit;
            }
            //处理用户扫一扫
            if ($event['event'] == "SCAN"){
                exit;
            }
            //处理用户的输入
            if($content){
                echo $weixin->kefu()->reply();
                exit;
            }
        }else{
            echo "";
            exit;
        }
    }
    function updatelocation($wxid,$info){
        $wx_user = $this->wx_user->where("open_id='$wxid'")->field("uid")->find();
        if(count($wx_user)>0 && $wx_user['uid']>0){
            $wx_user['lat'] = $info['Latitude'];
            $wx_user['lon'] = $info['Longitude'];
            $this->wx_user->save($wx_user);
            $sql = $this->wx_user->getLastSql();
        }
        return true;
    }
    //关注
    function followUser($wxid,$info=array()){
    	$promoters_id = $info['promoters_id'];  //推广者ID
        $nickname = $info['nickname'];
        $sex = intval($info['sex']);
        $country = $info['country'];
        $province = $info['province'];
        $city = $info['city'];
        $access_token = $info['access_token'];
        $headimgurl = $info['headimgurl'];
        $expire_in = time()+48*3600;
        $wx_user = $this->wx_user->where("open_id='$wxid'")->field("uid")->find();
        $from_id = intval($_GET['id']) > 0 ? intval($_GET['id']) : 1 ;
        if(count($wx_user)>0 && $wx_user['uid']>0){
            $wx_user['open_id'] = $wxid;
            $wx_user['nickname'] = $nickname;
            $wx_user['sex'] = $sex;
            $wx_user['country'] = $country;
            $wx_user['province'] = $province;
            $wx_user['city'] = $city;
            $wx_user['access_token'] = $access_token;
            $wx_user['expire_in'] = $expire_in;
            $wx_user['headimgurl'] = $headimgurl;
            $wx_user['from_id'] = $from_id;
            $wx_user['isfollow'] = 1;
            $result=$this->wx_user->save($wx_user);
        }else{
            $createtime = time();
            $createymd = date('Y-m-d');
            $wx_user['open_id'] = $wxid;
            $wx_user['nickname'] = $nickname;
            $wx_user['sex'] = $sex;
            $wx_user['country'] = $country;
            $wx_user['province'] = $province;
            $wx_user['city'] = $city;
            $wx_user['access_token'] = $access_token;
            $wx_user['expire_in'] = $expire_in;
            $wx_user['headimgurl'] = $headimgurl;
            $wx_user['createtime'] = $createtime;
            $wx_user['createymd'] = $createymd;
            $wx_user['isfollow'] = 1;
            $wx_user['from_id'] = $from_id;
            $wx_user['promoters_id'] = $promoters_id;
            
            $result=$this->wx_user->add($wx_user);
        }
        return true;
    }
}

